//
//  FirstTopViewViewController.m
//  Project_B
//
//  Created by 张闯的mac pro on 15/11/25.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "FirstTopViewViewController.h"
#import "AFNetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "FirstModel.h"
#import "CalculateHeight.h"
#import "CommentTableViewCell.h"

@interface FirstTopViewViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *topTableView;
@property (nonatomic, retain) NSMutableDictionary *topDic;

@property (nonatomic, retain) NSMutableArray *commentArr;

@end

@implementation FirstTopViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.title = @"话题精选";
    self.topDic = [NSMutableDictionary dictionary];
    self.commentArr = [NSMutableArray array];
    
    [self getTopViewData];
    [self getCommentData];
    [self createTopTabelView];
    
    
}

- (void)createTopTabelView
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 280)];
    [self.view addSubview:headView];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 180)];
    imgView.tag = 1001;
    [headView addSubview:imgView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 185, 300, 30)];
    titleLabel.tag = 1002;
    titleLabel.textColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1];
    [headView addSubview:titleLabel];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 225, 300, 20)];
    timeLabel.tag = 1003;
    timeLabel.textColor = [UIColor grayColor];
    [timeLabel setFont:[UIFont systemFontOfSize:13]];
    [headView addSubview:timeLabel];
    
    UILabel *placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 250, 300, 20)];
    placeLabel.tag = 1004;
    placeLabel.textColor = [UIColor grayColor];
    [placeLabel setFont:[UIFont systemFontOfSize:13]];
    [headView addSubview:placeLabel];
    
    //tableView
    self.topTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.topTableView];
    self.topTableView.delegate = self;
    self.topTableView.dataSource = self;
    self.topTableView.tableHeaderView = headView;
    
    [self.topTableView registerClass:[CommentTableViewCell class] forCellReuseIdentifier:@"cell"];
}

//获取headView数据
- (void)getTopViewData
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v3/club/topics/%@/", self.idString];
    
    NSString *bodyStr = [NSString stringWithFormat:@"topic_id=%@", self.idString];
    NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [AFNetworkHandler requestPOSTWithURL:str body:dataBody comletion:^(id result) {
        
        self.topDic = result[@"data"];
        [self getHeaderViewData:self.topDic];
        [self.topTableView reloadData];
    }];
}

- (void)getHeaderViewData:(NSDictionary *)dic
{
    UIImageView *imgView = (UIImageView *)[self.view viewWithTag:1001];
    [imgView sd_setImageWithURL:[NSURL URLWithString:((NSArray *)dic[@"topic_image"]).firstObject[@"image"]] placeholderImage:[UIImage imageNamed:@""]];
    
    UILabel *titleLabel = (UILabel *)[self.view viewWithTag:1002];
    titleLabel.text = dic[@"title"];
    
    UILabel *timeLabel = (UILabel *)[self.view viewWithTag:1003];
    timeLabel.text = [NSString stringWithFormat:@"活动时间: %@", dic[@"times"]];
    
    UILabel *placrLabel = (UILabel *)[self.view viewWithTag:1004];
    placrLabel.text = [NSString stringWithFormat:@"活动地点: %@", dic[@"aimaddress"]];
}

//获取评论区数据
- (void)getCommentData
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/replylist/%@/?page=0?page=0&topic_id=%@", self.idString, self.idString];
    
    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
       
        self.commentArr = [FirstModel transformWithArray:result[@"data"][@"topiclist"]];
        [self.topTableView reloadData];
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.commentArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h = [CalculateHeight heightWithText:((FirstModel *)self.commentArr[indexPath.row]).content fontSize:13];
    
    FirstModel *temp = [[FirstModel alloc] init];
    temp = self.commentArr[indexPath.row];
    CGFloat h1 = [((NSArray *)temp.topic_image).firstObject[@"height"] floatValue];
    CGFloat w1 = [((NSArray *)temp.topic_image).firstObject[@"width"] floatValue];
    CGFloat sum = h + 100 + h1;
    
    return sum;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
